package com.decent.ejfadmin.user.dao;

import com.decent.ejfadmin.common.bean.Announcement;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 会员公告
 *
 * @author weicx
 * @date 2018/07/26
 */
@Mapper
@Repository
public interface AnnouncementDao {
    /**
     * 新增公告
     *
     * @param announcement 公告实体
     * @return 新增行数
     */
    @Insert("insert into yuhuang_announcement (date,title,content,author,type,readable,admin,model_id) values " +
            "(now(),#{announcement.title},#{announcement.content},#{announcement.author},#{announcement.type}," +
            "#{announcement.readable},#{announcement.admin},#{announcement.modelId})")
    int createNewAnnouncement(@Param("announcement") Announcement announcement);

    /**
     * 分页查询公告
     *
     * @param pageBegin 页数
     * @param pageEnd   每页条数
     * @param start     起始时间
     * @param end       结束时间
     * @return list
     */
    @Select("select a.id,a.title ,a.admin ,a.author author,a.type type,DATE_FORMAT(a.date,'%Y-%m-%d %H:%i:%S') date ,a.content ," +
            "a.readable,a.status,a.model_id modelId " +
            "from yuhuang_announcement a where  date >= #{start} and date <= #{end} " +
            "order by a.status,a.id desc limit #{pageBegin},#{pageEnd} ")
    List<Announcement> queryAnnouncementPage(@Param("pageBegin") Integer pageBegin, @Param("pageEnd") Integer pageEnd, @Param("start") String start, @Param("end") String end);

    /**
     * 查询公告条数
     *
     * @param start 起始时间
     * @param end   结束时间
     * @return 总数
     */
    @Select("select count(1) from yuhuang_announcement where date >= #{start} and date <= #{end}")
    int queryAnnouncementCount(@Param("start") String start, @Param("end") String end);

    /**
     * 修改公告
     *
     * @param announcement 公告实体
     * @return 新增行数
     */
    @Update("update yuhuang_announcement set date = #{announcement.date},title = #{announcement.title}," +
            "content = #{announcement.content},author = #{announcement.author},type = #{announcement.type},readable = #{announcement.readable},model_id = #{announcement.modelId}," +
            "admin = #{announcement.admin} where id = #{announcement.id}")
    int modifyAnnouncement(@Param("announcement") Announcement announcement);

    /**
     * 删除公告
     *
     * @param announcement 公告实体
     * @return 删除行数
     */
    @Update("update yuhuang_announcement set date=#{announcement.date},status=1 where id = #{announcement.id}")
    int deleteAnnouncement(@Param("announcement") Announcement announcement);

    /**
     * 新增公告模板
     *
     * @param announcement 公告实体
     * @return 新增行数
     */
    @Insert("insert into yuhuang_announcement_model (model_title,model_content) values (#{announcement.title},#{announcement.content})")
    int addAnnouncementModel(@Param("announcement") Announcement announcement);

    /**
     * 修改公告模板
     *
     * @param announcement 公告实体
     * @return 新增行数
     */
    @Update("update  yuhuang_announcement_model set model_title=#{announcement.title},model_content=#{announcement.content} where id=#{announcement.id}")
    int updateAnnouncementModel(@Param("announcement") Announcement announcement);

    /**
     * 查询公告模板
     *
     * @return
     */
    @Select("select id,model_title title,model_content  content from yuhuang_announcement_model")
    List<Announcement> getAnnouncementModel();

    /**
     * 查询公告模板内容
     *
     * @param id
     * @return
     */
    @Select("select model_content  content from yuhuang_announcement_model where id=#{id}")
    String getAnnouncementModelContent(@Param("id") Integer id);

    /**
     * 查询公告列表
     *
     * @param id 公告Id
     * @return 公告信息
     */
    @Select(" select a.id,a.title ,a.admin ,a.author author,a.type type,DATE_FORMAT(a.date,'%Y-%m-%d %H:%i:%S') date ,a.content ," +
            " a.readable,a.status,a.model_id modelId " +
            " from yuhuang_announcement a where  a.id = #{id} ")
    Announcement queryAnnouncementById(@Param("id") Integer id);
}
